<?php

namespace Modules\Wechat\Model\Data\OfficialAccount;

use Modules\Wechat\Model\Dao\Db\WcOaUser;
use Modules\Wechat\Model\Data\Abstraction as DataAbstraction;

/**
 * 用户授权后获取的用户信息
 *
 * @package Modules\Wechat\Model\Data\OfficialAccount
 */
class User extends DataAbstraction
{
    const DICT_SCOPE_LANG = 'lang';
    const DICT_SCOPE_SEX  = 'sex';


    protected static $dictionaries = [
        self::DICT_SCOPE_LANG => [
            [self::DICT_ENTRY_ID => '1', self::DICT_ENTRY_VALUE => '男'],
            [self::DICT_ENTRY_ID => '2', self::DICT_ENTRY_VALUE => '女'],
            [self::DICT_ENTRY_ID => '0', self::DICT_ENTRY_VALUE => '未知'],
        ],
        self::DICT_SCOPE_LANG => [
            [self::DICT_ENTRY_ID => 'zh_CN', self::DICT_ENTRY_VALUE => '简体中文'],
            [self::DICT_ENTRY_ID => 'zh_TW', self::DICT_ENTRY_VALUE => '繁体中文'],
            [self::DICT_ENTRY_ID => 'en', self::DICT_ENTRY_VALUE => '英语'],
        ],
    ];

    /**
     * 获取语言名称:如"简体"
     *
     * @param string $lang_id
     *
     * @return string
     */
    public static function getLangName($lang_id)
    {
        return static::dictGetEntryNameById(static::DICT_SCOPE_LANG, $lang_id);
    }

    /**
     * 获取性别名称:如"男"
     *
     * @param int $sex_id
     *
     * @return string
     */
    public static function getSexName($sex_id)
    {
        return static::dictGetEntryNameById(static::DICT_SCOPE_SEX, $sex_id);
    }

    /**
     * 保存用户信息
     *
     * 参数为null时不保存也不更新.
     *
     * @param string      $official_account
     * @param string      $open_id
     * @param string|null $union_id
     * @param string|null $subscribe
     * @param string|null $subscribe_time
     * @param string|null $group_id
     * @param string|null $sex
     * @param string|null $language
     * @param string|null $nickname
     * @param string|null $city
     * @param string|null $province
     * @param string|null $country
     * @param string|null $head_img_url
     * @param string|null $remark
     *
     * @return bool|int
     */
    public function save(
        $official_account,
        $open_id,
        $union_id = null,
        $subscribe = null,
        $subscribe_time = null,
        $group_id = null,
        $sex = null,
        $language = null,
        $nickname = null,
        $city = null,
        $province = null,
        $country = null,
        $head_img_url = null,
        $remark = null
    ) {
        $WcOaUser = new WcOaUser;
        $WcOaUser->setPkValue($WcOaUser->generatePk($official_account, $open_id));

        $WcOaUser->official_account = $official_account;
        $WcOaUser->open_id          = $open_id;
        $WcOaUser->union_id         = $union_id;
        $WcOaUser->subscribe        = $subscribe;
        $WcOaUser->subscribe_time   = $subscribe_time;
        $WcOaUser->group_id         = $group_id;
        $WcOaUser->sex              = $sex;
        $WcOaUser->language         = $language;
        $WcOaUser->nickname         = $nickname;
        $WcOaUser->city             = $city;
        $WcOaUser->province         = $province;
        $WcOaUser->country          = $country;
        $WcOaUser->head_img_url     = $head_img_url;
        $WcOaUser->remark           = $remark;

        if ($exists = $this->existsById($WcOaUser->id)) {
            $WcOaUser->utime = time();
            return $WcOaUser->updateByPk();
        }
        $WcOaUser->ctime = time();
        return $WcOaUser->create(true);
    }

    /**
     * 通过数据主键检查数据是否存在
     *
     * @param string $id 主键
     *
     * @return array
     */
    public function existsById($id)
    {
        return (new WcOaUser)->readByPk($id) ?: [];
    }

    /**
     * 通过UnionID获取一条用户信息
     *
     * @param string $union_id
     *
     * @return array
     */
    public function getByUnionId($union_id)
    {
        $data = (new WcOaUser)->setProperty('union_id', $union_id)->readRow();
        return $data ? $this->processDetail($data) : [];
    }

    protected function processDetail(array $row)
    {
        $row['openid']        = $row['open_id'];
        $row['unionid']       = $row['union_id'];
        $row['headimgurl']    = $row['head_img_url'];
        $row['sex_name']      = static::getSexName($row['sex']);
        $row['language_name'] = static::getLangName($row['language']);
        return $row;
    }

    protected function init()
    {
        return $this;
    }

}